
/* 
引入说明

*/

const result= require('../util/result/result');

class baseController{
    constructor(service){
        this.service = service
    }

    async create(req,res){
        let data 
        try {
            data =  await  this.service.create(req.body)
        } catch (error) {
            console.log(error);
            return
        }
        if(data==undefined){
            res.send(result.result(false))
        }else{
            res.send(result.result(true,data))
        }
    }

   async findOne(req,res,grass){
    let data  
    try {
        data =  await  this.service.findOneService(grass)
    } catch (error) {
        // res.send(result.result(false,error))
    }

    if(data==undefined){
        res.send(result.result(false))
    }else{
        res.send(result.result(true,data.kemuIDArray))
    }
   }

    async aggregateController(req,res,grass){
        
        let data  
        try {
            data =  await  this.service.aggregateService(grass)
        } catch (error) {
            // res.send(result.result(false,error))
        }

        if(data==undefined){
            res.send(result.result(false))
        }else{
            res.send(result.result(true,data))
        }

    }

    async findByIdAndRemove(req,res){
   
        let data  
        try {
            data =  await  this.service.findByIdAndRemove(req.body)
        } catch (error) {
            // res.send(result.result(false,error))
        }

        if(data==undefined){
            res.send(result.result(false))
        }else{
            res.send(result.result(true,data))
        }
    }

    async findOneByObjAndDelete(req,res){

        let obj = req.body.obj
   
        let data  
        try {
            data =  await  this.service.findOneAndDelete(obj)
        } catch (error) {
            // res.send(result.result(false,error))
        }

        if(data==undefined){
            res.send(result.result(false))
        }else{
            res.send(result.result(true,data))
        }
    }

    

    async findOneAndUpdate(req,res,conditions,updates){
        let data
        try {
            data =  await  this.service.findOneAndUpdate(conditions,updates)
        } catch (error) {
            console.log(error);
            // res.send(result.result(false,error))
        }

        if(data&&data!=undefined){
            res.send(result.result(true,data))
         }else{
            res.send(result.result(false))
         }

    }



    async findByIdAndUpdate(req,res){
        let data  
        let id = req.body.id
        let updateContent =req.body.updateContent
        try {
            data =  await  this.service.findByIdAndUpdate(id,updateContent)
        } catch (error) {
            console.log(error);
            // res.send(result.result(false,error))
        }

        if(data&&data!=undefined){
            res.send(result.result(true,data))
         }else{
            res.send(result.result(false))
         }

    }

    async queryAll(req,res){
        let data  
        try {
            data =  await  this.service.queryAll()
        } catch (error) {
                console.log(error)
            return 
        }
        if(data&&data!=undefined){
            res.send(result.result(true,data))
         }else{
            res.send(result.result(false))
         }
    }



    async queryAll2(req,res,conditions,hides,fills,skip,limit){
        let data  
        try {
            data =  await  this.service.queryAll2(conditions,hides,fills,skip,limit)
        } catch (error) {
                console.log(error)
            return 
        }
        if(data&&data!=undefined){
            res.send(result.result(true,data))
         }else{
            res.send(result.result(false))
         }
    }


    // andPopulate
    /* 
   {
    obj:{id:'ffds43434'},
    param1:'userid',
    param2:'professionid'
   }
    
    */
   async  findByObj(req,res){
    let data 
    let obj = req.body.obj
    let param1 = req.body.param1
    let param2 = req.body.param2
    try {
        data =  await  this.service.findAndPopulate(obj,param1,param2)
    } catch (error) {
        res.send(result.result(false,error))
        return
    }
    if(data&&data!=undefined){
        res.send(result.result(true,data))
     }else{
        res.send(result.result(false))
     }
   }

   async  findOneByObjAndPopulate(req,res,param1,param2){
    let data 
    let obj = req.body.obj
    // let param1 = req.body.param1
    // let param2 = req.body.param2
    try {
        data =  await  this.service.findOneAndPopulate(obj,param1,param2)
    } catch (error) {
        res.send(result.result(false,error))
        return
    }
    if(data&&data!=undefined){
        res.send(result.result(true,data))
     }else{
        res.send(result.result(false))
     }
   }

   /* 
   入参:
   id,查找的id
   param1,填充的key
   param2,填充的key
   示例
   {
    id:'dsdasda13',
    param1:'user',
    param2:'profession'
   }
   */
   async findByIdAndPopulate (req,res){
    let data
    let id = req.body.id
    let param1 = req.body.param1
    let param2 = req.body.param2
    try {
        data =  await  this.service.findByIdAndPopulate(id,param1,param2)
    } catch (error) {
        
        res.send(result.result(false,error))
        return
    }
    if(data&&data!=undefined){
        res.send(result.result(true,data))
     }else{
        res.send(result.result(false))
     }
   }



   async findByObjAndUpdate (req,res){
 
    let data
    try {
        data =  await  this.service.findByObjAndUpdate(req.body.obj,req.body.update_content)
    } catch (error) {
       
        res.send(result.result(false,error))
        return
    }
  
    if(data&&data!=undefined){
        res.send(result.result(true,data))
     }else{
        res.send(result.result(false))
     }
   }


}

module.exports={
    baseController
}


